﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Xsl;

namespace Community.DynamicsCrmWebResourceTool.Models
{
    public class FileMap
    {
        public Guid Id { get; set; }
        public string FileName { get; set; }
        public string WebResourceName { get; set; }
        public string SolutionId { get; set; }
        public DateTime LastUpload { get; set; }
        public DateTime LastPublish { get; set; }
        /// <summary>
        /// To map to webresource in crm
        /// </summary>
        public Guid WebResourceId { get; set; }
        public string WebResourceDisplayName { get; set; }
        public string WebResourceDescription { get; set; }

        internal XmlDocument XmlDoc { get; set; }

        internal void Save()
        {
            if (XmlDoc != null)
            {
                if (Id.Equals(Guid.Empty))
                {
                    Id = Guid.NewGuid();
                    XmlNode mapList = XmlDoc.SelectSingleNode("//Settings/Mappings");

                    XmlNode newMap = XmlDoc.CreateNode(XmlNodeType.Element, "", "Mapping", string.Empty);

                    AddMappingAttribute("Id", Id.ToString(), newMap);

                    AddMappingAttribute("FileName", FileName, newMap);

                    AddMappingAttribute("WebResourceName", WebResourceName, newMap);

                    AddMappingAttribute("SolutionId", SolutionId, newMap);

                    AddMappingAttribute("LastUpload", LastUpload.ToString("yyyy-MM-dd HH:mm:ss"), newMap);

                    AddMappingAttribute("LastPublish", LastPublish.ToString("yyyy-MM-dd HH:mm:ss"), newMap);

                    AddMappingAttribute("WebResourceId", WebResourceId.ToString(), newMap);
                    
                    mapList.AppendChild(newMap);
                }
                else
                {
                    XmlNode map = XmlDoc.SelectSingleNode("//Settings/Mappings/Mapping[@Id='" + Id.ToString().Replace("{", "").Replace("}", "") + "']");
                    if (map != null)
                    {
                        map.Attributes["FileName"].Value = FileName;
                        map.Attributes["WebResourceName"].Value = WebResourceName;
                        map.Attributes["SolutionId"].Value = SolutionId;
                        map.Attributes["LastUpload"].Value = LastUpload.ToString("yyyy-MM-dd HH:mm:ss");
                        map.Attributes["LastPublish"].Value = LastPublish.ToString("yyyy-MM-dd HH:mm:ss");
                        map.Attributes["WebResourceId"].Value = WebResourceId.ToString();
                    }
                }

                return;
            }

            throw new Exception("XmlDoc is not set. Please initialize it before calling Save()");
        }

        private void AddMappingAttribute(string attributeName, string value, XmlNode parentNode)
        {
            if (parentNode != null)
            {
                XmlNode newAttribute = XmlDoc.CreateNode(XmlNodeType.Attribute, "", attributeName, string.Empty);
                newAttribute.Value = value;
                parentNode.Attributes.SetNamedItem(newAttribute);
            }
        }
    }
}
